package com.mes.schedule.dao;

import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.common.dao.BasicDao;
import com.mes.common.util.Page;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.mes.schedule.dao.iface.IDemandSTaskLinkDao;
import com.mes.schedule.domain.DemandSTaskLink;
import com.mes.task.domain.Demand;
import com.mes.task.domain.DemandTaskLink;

@Repository
public class DemandSTaskLinkDao extends BasicDao<DemandSTaskLink> implements IDemandSTaskLinkDao{
	private static final Log log = LogFactory.getLog(DemandSTaskLinkDao.class);
	
	/**
	 * @author lcs
	 * @see the method use for DemandSTaskLinkManger
	 * @param masterShopUid
	 * @return
	 */
	public List<DemandSTaskLink> getUnLockedLinks(String masterShopUid) {
		List list = null;

		try {
			Session session = this.getSession();
			StringBuilder hql = new StringBuilder();
			hql.append(" from " + DemandSTaskLink.class.getName() + " where ");
			hql.append(" demand.demandState=" + Demand.DEMANDSTATE_STARTED);
			hql.append(" and demand.masterShopUid like ? || '%' ");
			hql.append(" and  not (lockLevel=" + DemandSTaskLink.LOCKLEVEL_LOCKED + ")");
			hql.append(" and isInEffect=" + DemandSTaskLink.ISINEFFECT_IS);

			Query query = session.createQuery(hql.toString());
			query.setString(0, masterShopUid);

			// if (page != null) page.pagePro(query);
			list = query.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * @author lcs
	 * @see the method use for DemandSTaskLinkManger
	 * @param scheduleUid
	 * @param demandUid
	 * @param partTaskUid
	 * @return
	 */
	public DemandSTaskLink getDemandTaskLink(String scheduleUid, String demandUid, String partTaskUid) {
		DemandSTaskLink demandSTaskLink = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DemandSTaskLink.class);
			crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			crit.add(Restrictions.eq("demandUid", demandUid));
			crit.add(Restrictions.eq("partTaskUid", partTaskUid));
			crit.setMaxResults(1);
			demandSTaskLink = (DemandSTaskLink) crit.uniqueResult();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return demandSTaskLink;

	}
	
	/**
	 * @author lcs
	 * @see the method use for DemandSTaskLinkManager
	 * @param scheduleUid
	 * @param demandUid
	 * @param page
	 * @return
	 */
	public List<DemandSTaskLink> getDemandTaskLinksByDemand(String scheduleUid, String demandUid, Page page) {
		List<DemandSTaskLink> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DemandSTaskLink.class);
			crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			crit.add(Restrictions.eq("demandUid", demandUid));
			// crit.add(Restrictions.eq("isInEffect",
			// DemandTaskLink.ISINEFFECT_IS));
			// 如果需要进行分页处理，则调用如下语句

			// crit.createAlias("partTask", "pt");
			// crit.addOrder(Property.forName("pt.lateFinish").desc());
			list = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * @author lcs
	 * @see the method use for DemandSTaskLinkManager
	 * @param scheduleUid
	 * @param taskUid
	 * @return
	 */
	public List<DemandSTaskLink> getDemandTaskLinksByTask(String scheduleUid, String taskUid) {

		List<DemandSTaskLink> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DemandSTaskLink.class);
			crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			crit.add(Restrictions.eq("partTaskUid", taskUid));
			crit.add(Restrictions.eq("isInEffect", DemandTaskLink.ISINEFFECT_IS));
			// 如果需要进行分页处理，则调用如下语句
			// crit.addOrder(Property.forName("demandUid").desc());
			list = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * @author lcs
	 * @see the method use for DemandSTaskLinkManager
	 * @param scheduleUid
	 * @param taskUid
	 * @return
	 */
	public List<DemandSTaskLink> getEffectiveLinksByTask(String scheduleUid, String taskUid) {

		List<DemandSTaskLink> list = new ArrayList<DemandSTaskLink>();
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DemandSTaskLink.class);
			crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			crit.add(Restrictions.eq("partTaskUid", taskUid));
			crit.add(Restrictions.eq("isInEffect", DemandSTaskLink.ISINEFFECT_IS));
			// 如果需要进行分页处理，则调用如下语句

			// crit.addOrder(Property.forName("demandUid").desc());
			list = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

}
